package com.example.controller;

import com.example.pojo.ResultBean;
import com.example.pojo.Specification;
import com.example.pojo.SpecificationOption;
import com.example.req.SpecificationGroup;
import com.example.req.SpecificationReq;
import com.example.service.SpecificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    @GetMapping("list")
    public ResultBean list(SpecificationReq specificationReq){
        System.out.println(specificationReq);
        PageHelper.startPage(specificationReq.getPageNum(),specificationReq.getPageSize());
        List<Specification> specifications = specificationService.queryByCondition(specificationReq);
        PageInfo pageInfo = new PageInfo(specifications);
        return ResultBean.success(pageInfo);
    }

    @GetMapping("/queryAll")
    public ResultBean queryAll(){
        List<Specification> brands = specificationService.queryByCondition(null);
        return ResultBean.success(brands);
    }

    @GetMapping("/querySpecificationOptionBySpecId/{id}")
    public ResultBean querySpecificationOptionBySpecId(@PathVariable("id") Long id){
        List<SpecificationOption> specificationOptions = specificationService.querySpecificationOptionBySpecId(id);
        return ResultBean.success(specificationOptions);
    }

    @PostMapping("/saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody SpecificationGroup specificationGroup){
        System.out.println("规格数据详情：" + specificationGroup);
        int rs = specificationService.saveOrUpdate(specificationGroup);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("服务器异常");
    }

    @DeleteMapping("/delete/{id}")
    public ResultBean delete(@PathVariable("id") Long id){
        System.out.println("要删除的id：" + id);
        int rs = specificationService.deleteById(id);
        return rs >= 1 ? ResultBean.success() : ResultBean.error("服务器异常");
    }
}
